package com.iris.live.services.controllers;

import com.alibaba.fastjson.JSON;
import com.iris.live.services.common.CommonUtil;
import com.iris.live.services.services.CommonSearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 报表检索视图转Api控制器
 *
 * @author  gengshuai
 *          2017/8/25
 */
@RestController
@RequestMapping(value = "/common/search")
public class SearchCommonController {

    private static final Logger logger = LoggerFactory.getLogger(SearchCommonController.class);

    @Autowired
    private CommonSearchService commonSearchService;

    /**
     * 获取用户所在大区
     *
     * @param comm
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/area/list", method = RequestMethod.POST)
    public Map<String, Object> getAreasByUserName(@RequestBody CommonSearchService.CommonSearchRequest comm) {

        try {
            //获取用户名所属大区LIST
            Map<String, Object> areaLists = commonSearchService.listAreas(comm);
            logger.info("[Common][Controller][GetAreaByUserName]: {"+ JSON.toJSONString(areaLists)+"}");
            return CommonUtil.setSuccessInfo(areaLists);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][Controller][GetAreaByUserName]:获取该大区数据出错" + e.getMessage());
            return CommonUtil.setErrorInfo("获取该大区数据出错");
        }
    }

    /**
     * PMA API
     *
     * @param comm
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/pma/list", method = RequestMethod.POST)
    public Map<String, Object> getPmasByUserName(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            //获取用户名所属PMA LIST
            Map<String, Object> areaLists = commonSearchService.listPmas(comm);
            logger.info("[Common][Controller][getPmaByUserName]: {"+ JSON.toJSONString(areaLists)+"}");
            return CommonUtil.setSuccessInfo(areaLists);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][Controller][getPmaByUserName]:获取该PMA数据出错" + e.getMessage());
            return CommonUtil.setErrorInfo("获取该PMA数据出错");
        }
    }

    /**
     * 经销商 API
     *
     * @param comm
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/dealer/list", method = RequestMethod.POST)
    public Map<String, Object> getDealersByUserName(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            //获取用户名所属经销商List
            Map<String, Object> areaLists = commonSearchService.listDealers(comm);
            logger.info("[Common][Controller][getDealersByUserName]: {"+ JSON.toJSONString(areaLists)+"}");
            return CommonUtil.setSuccessInfo(areaLists);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][Controller][getDealersByUserName]:获取该经销商数据出错" + e.getMessage());
            return CommonUtil.setErrorInfo("获取该经销商数据出错");
        }
    }

    /**
     * 获取活动类型API（视图->API接口）
     *
     * @return Map
     */
    @RequestMapping(value = "/activitytypes/list", method = RequestMethod.POST)

    public Map<String, Object> getActivityTypes(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> activityTypes = commonSearchService.getActivityTypes(comm);
            logger.info("[Common][SearchCommonController][getActivityTypes]: {"+ JSON.toJSON(activityTypes)+"}");
            return CommonUtil.setSuccessInfo(activityTypes);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getActivityTypes]:获取活动类型出错");
            return CommonUtil.setErrorInfo("获取活动类型出错");
        }
    }


    /**
     * 获取主导与发起API（视图->API接口）
     *
     * @return Map
     */
    @RequestMapping(value = "/leadingandlaunches/list", method = RequestMethod.POST)
    public Map<String, Object> getLeadingAndLaunches(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> leadingAndLaunches = commonSearchService.getLeadingAndLaunches(comm);
            logger.info("[Common][SearchCommonController][getLeadingAndLaunches]: {"+ JSON.toJSON(leadingAndLaunches)+"}");
            return CommonUtil.setSuccessInfo(leadingAndLaunches);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getLeadingAndLaunches]:获取主导与发起出错");
            return CommonUtil.setErrorInfo("获取主导与发起出错");
        }

    }

    /**
     * 获取车系API（视图->API接口）
     *
     * @return Map
     */
    @RequestMapping(value = "/carseries/list", method = RequestMethod.POST)
    public Map<String, Object> getCarSeries(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> getCarSeries = commonSearchService.getCarSeries(comm);
            logger.info("[Common][SearchCommonController][getCarSeries]: {"+ JSON.toJSON(getCarSeries)+"}");
            return CommonUtil.setSuccessInfo(getCarSeries);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getCarSeries]:获取车系出错");
            return CommonUtil.setErrorInfo("获取车系出错");
        }

    }

    /**
     * 获取车型API（视图->API接口）
     *
     * @return Map
     */
    @RequestMapping(value = "/carmodes/list", method = RequestMethod.POST)
    public Map<String, Object> getCarModes(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> getCarModes = commonSearchService.getCarModes(comm);
            logger.info("[Common][SearchCommonController][getCarModes]: {"+ JSON.toJSON(getCarModes)+"}");
            return CommonUtil.setSuccessInfo(getCarModes);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getCarModes]:获取车型出错");
            return CommonUtil.setErrorInfo("获取车系出错");
        }

    }

    /**
     * 获取金融贷款类型API（视图->API接口）
     *
     * @return Map
     */
    @RequestMapping(value = "/financeloantypes/list", method = RequestMethod.POST)
    public Map<String, Object> getFinanceLoanTypes(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> getFinanceLoanTypes = commonSearchService.getFinanceLoanTypes(comm);
            logger.info("[Common][SearchCommonController][getFinanceLoanTypes]: {"+ JSON.toJSON(getFinanceLoanTypes)+"}");
            return CommonUtil.setSuccessInfo(getFinanceLoanTypes);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getFinanceLoanTypes]:获取金融贷款类型出错");
            return CommonUtil.setErrorInfo("获取金融贷款类型出错");
        }

    }

    /**
     * 获取品牌API（视图->API接口）
     *
     * @return Map
     */
    @RequestMapping(value = "/brands/list", method = RequestMethod.POST)
    public Map<String, Object> getBrands(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> getBrands = commonSearchService.getBrands(comm);
            logger.info("[Common][SearchCommonController][getBrands]: {"+ JSON.toJSON(getBrands)+"}");
            return CommonUtil.setSuccessInfo(getBrands);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getBrands]:获取品牌出错");
            return CommonUtil.setErrorInfo("获取品牌出错");
        }

    }

    /**
     * 获取品牌合计API（视图->API接口）
     *
     * @return Map
     */
    @RequestMapping(value = "/brandtotals/list", method = RequestMethod.POST)
    public Map<String, Object> getBrandTotals(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> getBrandTotals = commonSearchService.getBrandTotals(comm);
            logger.info("[Common][SearchCommonController][getBrandTotals]: {"+ JSON.toJSON(getBrandTotals)+"}");
            return CommonUtil.setSuccessInfo(getBrandTotals);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getBrandTotals]:获取品牌合计出错");
            return CommonUtil.setErrorInfo("获取品牌合计出错");
        }

    }

    /**
     * 获取渠道API（视图->API接口）
     *
     * @return Map
     */
    @RequestMapping(value = "/channels/list", method = RequestMethod.POST)
    public Map<String, Object> getChannels(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> getChannels = commonSearchService.getChannels(comm);
            logger.info("[Common][SearchCommonController][getChannels]: {"+ JSON.toJSON(getChannels)+"}");
            return CommonUtil.setSuccessInfo(getChannels);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getChannels]:获取渠道出错");
            return CommonUtil.setErrorInfo("获取渠道出错");
        }

    }

    /**
     * 获取金融机构列表
     * @return
     */
    @RequestMapping(value = "/financialinstitution/list", method = RequestMethod.POST)
    public Map<String, Object> getFinancialInstitutions(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> financialInstitutions = commonSearchService.getFinancialInstitutions(comm);
            logger.info("[Common][SearchCommonController][getFinancialInstitutions]: {"+ JSON.toJSON(financialInstitutions)+"}");
            return CommonUtil.setSuccessInfo(financialInstitutions);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getFinancialInstitutions]:获取金融机构出错");
            return CommonUtil.setErrorInfo("获取金融机构出错");
        }

    }

    /**
     * 获取调研类型列表
     * @return
     */
    @RequestMapping(value = "/researchtype/list", method = RequestMethod.POST)
    public Map<String, Object> getResearchTypes(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> researchTypes = commonSearchService.getResearchTypes(comm);
            logger.info("[Common][SearchCommonController][getResearchTypes]: {"+ JSON.toJSON(researchTypes)+"}");
            return CommonUtil.setSuccessInfo(researchTypes);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getResearchTypes]:获取调研类型出错");
            return CommonUtil.setErrorInfo("获取调研类型出错");
        }

    }

    /**
     * 获取问卷版本号列表
     * @return
     */
    @RequestMapping(value = "/questionversion/list", method = RequestMethod.POST)
    public Map<String, Object> getQuestionVersion(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> questionVersion = commonSearchService.getQuestionVersion(comm);
            logger.info("[Common][SearchCommonController][getQuestionVersion]: {"+ JSON.toJSON(questionVersion)+"}");
            return CommonUtil.setSuccessInfo(questionVersion);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getQuestionVersion]:获取问卷版本号出错");
            return CommonUtil.setErrorInfo("获取问卷版本号出错");
        }

    }

    /**
     * 获取经销商销售顾问
     * @return
     */
    @RequestMapping(value = "/salesconsultants/list", method = RequestMethod.POST)
    public Map<String, Object> getSalesConsultants(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> salesConsultants = commonSearchService.getSalesConsultants(comm);
            logger.info("[Common][SearchCommonController][getSalesConsultants]: {"+ JSON.toJSON(salesConsultants)+"}");
            return CommonUtil.setSuccessInfo(salesConsultants);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getSalesConsultants]:获取经销商销售顾问出错");
            return CommonUtil.setErrorInfo("获取经销商销售顾问出错");
        }

    }

    /**
     * 根据用户名取许可
     * @return
     */
    @RequestMapping(value = "/getpermitbyusername", method = RequestMethod.POST)
    public Map<String, Object> getPermitByUserName(@RequestBody CommonSearchService.CommonSearchRequest comm) {
        try {
            Map<String, Object> permitMap = commonSearchService.getPermitByUserName(comm);
            logger.info("[Common][SearchCommonController][getPermitByUserName]: {"+ JSON.toJSON(permitMap)+"}");
            return CommonUtil.setSuccessInfo(permitMap);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("[Common][SearchCommonController][getPermitByUserName]:根据用户名取许可出错");
            return CommonUtil.setErrorInfo("根据用户名取许可出错");
        }

    }

    /**
     * test api
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/direct/test", method = RequestMethod.GET)
    public Map<String, Object> listRoomType() throws Exception {
        return CommonUtil.setSuccessInfo();
    }

}
